"use strict";
import axios from "axios"
import { Message } from 'element-ui'
const BASE_URL = process.env.VUE_APP_BASE_URL

// response 拦截器
// 可以在接口响应后统一处理结果
const request = axios.create({
    baseURL: `${BASE_URL}`,
    timeout: 500000
})
request.interceptors.request.use(
    config => {
        const timestamp = Date.now();
        config.url = config.url.indexOf('?') === -1 ? `${config.url}?time=${timestamp}` : `${config.url}&time=${timestamp}`
        return config;
    }, error => {
        // tryHideFullScreenLoading();
        return error
    }
)

request.interceptors.response.use(
    response => {
        if (response.data.errors) {
            if (response.data.errors[0].message) {
                Message.error(response.data.errors[0].message);
                if (response.data.errors[0].message.includes('当前用户未登录，请先登录')) {
                    window.location.replace(
                        `${window.location.origin}/#/login`
                    );
                }
            } else {
                Message.error('服务器繁忙，请稍后再试');
            }
        }
        return response;
    }, error => {
        // tryHideFullScreenLoading();

        // Do something with response error

        return Promise.reject(error);
    }
);
//导出
export default request
